package com.czkt.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Map;

@Component
public class ConsumerController {

//    private Integer count = 0;
//
//    @RabbitListener(queues = "queue")
//    public void recipient(byte[] messageBytes){
//        count++;
//        try {
//            // 将字节数组转换为输入流
//            ByteArrayInputStream bis = new ByteArrayInputStream(messageBytes);
//            ObjectInputStream ois = new ObjectInputStream(bis);
//            // 从输入流中读取对象
//            Map<String, Object> message = (Map<String, Object>) ois.readObject();
//            // 关闭输入流
//            ois.close();
//            bis.close();
//            // 现在可以处理消息了
//            System.out.println("消费者1收到第" + count + "条" + message.get("email"));
//            Thread.sleep(10);
//        } catch (IOException | ClassNotFoundException e) {
//            e.printStackTrace();
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//    }

//    @RabbitListener(queues = "queue")
//    public void recipient1(byte[] messageBytes){
//        count++;
//        try {
//            // 将字节数组转换为输入流
//            ByteArrayInputStream bis = new ByteArrayInputStream(messageBytes);
//            ObjectInputStream ois = new ObjectInputStream(bis);
//
//            // 从输入流中读取对象
//            Map<String, Object> message = (Map<String, Object>) ois.readObject();
//
//            // 关闭输入流
//            ois.close();
//            bis.close();
//            // 现在可以处理消息了
//            System.out.println("消费者2收到第" + count + "条" + message.get("email"));
//            Thread.sleep(200);
//        } catch (IOException | ClassNotFoundException e) {
//            e.printStackTrace();
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//    }

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = "simple.queue"),exchange = @Exchange(value = "activityInfo"),key = "vip")
//    )
//    public void receiveMessage1(String message) throws InterruptedException {
//        System.out.println("VIP消费者收到：" + message);
//        Thread.sleep(1000);
//    }

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = "simple.queue"),exchange = @Exchange(value = "activityInfo"),key = "ordinary")
//    )
//    public void receiveMessage2(String message) throws InterruptedException {
//        System.out.println("普通消费者收到：" + message);
//        Thread.sleep(1000);
//    }

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = "fanout.queue1"),exchange = @Exchange(value = "fanout.exchange",type = ExchangeTypes.FANOUT))
//    )
//    public void receiveMessage1(String message) {
//        System.out.println("VIP消费者收到：" + message);
//    }
//
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = "fanout.queue2"),exchange = @Exchange(value = "fanout.exchange",type = ExchangeTypes.FANOUT))
//    )
//    public void receiveMessage2(String message) {
//        System.out.println("普通消费者收到：" + message);
//    }

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = "direct.queue1"),exchange = @Exchange(value = "direct.exchange"),key = "vip")
//    )
//    public void receiveMessage1(String message) {
//        System.out.println("VIP消费者收到：" + message);
//    }
//
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = "direct.queue1"),exchange = @Exchange(value = "direct.exchange"),key = "ordinary")
//    )
//    public void receiveMessage2(String message) {
//        System.out.println("普通消费者收到：" + message);
//    }
//
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = "direct.queue2"),exchange = @Exchange(value = "direct.exchange"),key = "ordinary")
//    )
//    public void receiveMessage3(String message) {
//        System.out.println("普通消费者收到：" + message);
//    }

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = "topic.queue1"),exchange = @Exchange(value = "topic.exchange"),key = "vip.*")
//    )
//    public void receiveMessage1(String message) {
//        System.out.println("topic.queue1消费者收到：" + message);
//    }
//
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = "topic.queue1"),exchange = @Exchange(value = "topic.exchange"),key = "vip.#")
//    )
//    public void receiveMessage2(String message) {
//        System.out.println("topic.queue1消费者收到：" + message);
//    }
//
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = "topic.queue2"),exchange = @Exchange(value = "topic.exchange"),key = "ordinary.*")
//    )
//    public void receiveMessage3(String message) {
//        System.out.println("topic.queue2消费者收到：" + message);
//    }

}
